<!--电子围栏地图页-->
<template>
  <div style="height: 6vh;background-color:#fff;padding:10px 20px;">
    <span style="line-height:60px;font-size:20px;">新增围栏</span>
    <!-- 为返回按钮实现点击返回至上一页"电子围栏管理页" -->
    <el-button @click="router.push('/geofence')" type="primary" style="float:right;margin-top:13px;">返回</el-button>
  </div>
<!--  创建地图容器-->
  <div style="width: 100%; height:84vh" id="mapContainer">

  </div>

  <el-dialog v-model="dialogVisible" title="围栏信息" :before-close="closeDialog">
    <el-form  label-width="80px">
      <el-foem-item label="围栏名称">
        <el-input v-model="geofence.name" placeholder="请输入围栏名称"></el-input>
      </el-foem-item>
    </el-form>
    <template #footer>
      <el-button @click="closeDialog">取消</el-button>
      <el-button type="primary" @click="saveGeofence" >确定</el-button>
    </template>
  </el-dialog>
</template>

<script setup>
import router from "@/router";
import {nextTick, onMounted, ref} from "vue";
import { DrawScene, DrawControl,OperateEventType } from 'bmap-draw';
import axios from "axios";
import {ElMessage} from "element-plus";
import qs from "qs";


//定义电子围栏对象存储数据
const geofence=ref({
  name:'',
  position:{}
});
var map;

onMounted(()=>{
  //dom更新之后执行,地图在容器创建以后渲染
  nextTick(()=>{
    map=new BMapGL.Map("mapContainer");
    let point=new BMapGL.Point(116.4074,39.9042);
    map.centerAndZoom(point,15);
    // 添加鼠标绘制控件
    const scene = new DrawScene(map);
    const drawContrl = new DrawControl(scene, {
      enableTips: false,
      anchor: BMAP_ANCHOR_TOP_RIGHT,
      drawingItems: [
        'circle',
        'rectangle'
      ]
    });

    //围栏绘制结束添加监听事件,返回电子围栏对象
    scene.addEventListener(OperateEventType.COMPLETE,(event)=>{
      dialogVisible.value=true;
      console.log(event.target);
      let overlay=event.target.overlay;
      if (overlay instanceof BMapGL.Circle){
        geofence.value.position.type='circle';
        geofence.value.position.longitude=overlay.getCenter().lng;
        geofence.value.position.latitude=overlay.getCenter().lat;
        geofence.value.position.radius=overlay.getRadius();
        console.log(geofence.value.position);
      }else {
        //定义数组用来保存矩形的四个顶点坐标
        let recPoints = [];
        //依次获取矩形的四个顶点坐标并保存到数组中
        for(let i = 0; i < 4; i++){
          //overlay.points[i] 5个点 0,1,2,3循环四次,也就是依次获取四个顶点的经纬度值，存入recPoints数组中
          recPoints.push(overlay.points[i].latLng.lng+'-'+overlay.points[i].latLng.lat);
        }
        //console.log(recPoints);
        //将数组中的元素用逗号分隔并保存到data对象中
        //console.log(recPoints.join(','));
        //设置围栏类型为矩形
        geofence.value.position.type='rectangle';
        //将顶点坐标数组中的元素用逗号分隔并保存到position属性里
        geofence.value.position.recPoints = recPoints.join(',');
        console.log(geofence.value.position);
      }
    });
    //为地图添加控件
    map.addControl(drawContrl);

  });
})
//添加围栏的弹窗显示
const dialogVisible=ref(false);
//取消围栏
const closeDialog=()=>{
  if (confirm('确认取消吗')){
    dialogVisible.value=false;
    map.clearOverlays();
  }
};
//新建围栏
const saveGeofence=()=> {
  let data = qs.stringify(geofence.value);
  console.log("新增围栏数据:" + data);
  if (!geofence.value.name) {
    ElMessage.error('围栏名称不能为空');
    return;
  }else {
    axios.post(BASE_URL + '/v1/geofence/insert', data)
        .then((res) => {
          if (res.data.code == 2000) {
            ElMessage.success('添加围栏成功');
            dialogVisible.value=false;
            router.push('/geofence');
          } else {
            ElMessage.error(res.data.msg);
          }
        });
  }
}


</script>

<style scoped>
</style>